package com.budwk.app.sqfwV2.service.childrenVisitation;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.component.CommonComponent;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.vo.GenerateExcelUrlVO;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseExcelParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.ExcelGeneratorConfig;
import com.budwk.app.sqfwDoc.codeGenerator.impl.SqfwTemplateExcelCommonService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwV2.component.ChildrenVisitationComponent;
import com.budwk.app.sqfwV2.component.DocumentDownComponent;
import com.budwk.app.sqfwV2.dto.dto.childrenVisits.ChildrenVisitationPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownExcelLedgerReqVo;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-04-23
 *
 * 白云区儿童探访记录 ChildrenVisitation
 */
@IocBean
@Slf4j
public class ChildrenVisitationExcelService extends SqfwTemplateExcelCommonService {

    @Override
    public List<GenerateExcelUrlVO> generateExcel(List<ExcelCreateConfig> excelConfigList, String userId, List<String> sqfwIdList) {
        List<GenerateExcelUrlVO> urlVOS = super.generateExcel(excelConfigList, userId, sqfwIdList);
        return urlVOS;
    }

    public List<GenerateExcelUrlVO> downExcelLedger(DownExcelLedgerReqVo reqVo, List<ExcelCreateConfig> excelConfigList, String userId, List<Sqfw_formSummary_new> list) {
        // 1 检查是否已经下载
        List<GenerateExcelUrlVO> res = new ArrayList<>();
        String ids = CommonComponent.getEntityIds(list, reqVo.getSqfwIds(), SupportBusinessTypeEnum.CHILDREN_VISITATION.getName());
        List<ExcelCreateConfig> createConfigs = DocumentDownComponent.checkIsDownFile(excelConfigList, userId, ids, res, "2");
        if (createConfigs.size() == 0) {
            return res;
        }
        // 2 没有下载 重新下载
        List<GenerateExcelUrlVO> urlVOS = super.generateExcel(createConfigs, userId, reqVo.getSqfwIds());
        // 3 记录下载日志
        DocumentDownComponent.addGenerateExcel(createConfigs, urlVOS, userId, "2", ids);
        return urlVOS;

    }


    @Override
    protected void customerPopularBaseExcelParam(BaseExcelParamDTO baseExcelParam, String userId, List<String> sqfwIdList) {

    }

    @Override
    protected void customerPopularBaseExcelParamForAdmin(BaseExcelParamDTO baseExcelParam, String userId, List<String> formSummaryNewIdList) {

    }

    @Override
    protected List<ExcelGeneratorConfig> getExcelCodeGeneratorConfig(List<ExcelCreateConfig> excelConfigList, BaseExcelParamDTO baseExcelParam) {
        if (excelConfigList.size() == 0) {
            return Collections.emptyList();
        }
        // 查询大营路EXCEL台账合集字典数据
        List<ExcelGeneratorConfig> list = new ArrayList<>();
        for (ExcelCreateConfig config : excelConfigList) {
            ExcelGeneratorConfig generatorConfig = new ExcelGeneratorConfig();
            BaseTemplateExcelGenerator service = ChildrenVisitationComponent.getService(config.getFileIdentity());
            generatorConfig.setGenerator(service);
            generatorConfig.setExcelCreateConfig(config);
            list.add(generatorConfig);
        }
        return list;
    }

    @Override
    protected AbstractCustomerPdfCreateField parseParamData(String paramDataStr) {
        return JSONObject.parseObject(paramDataStr, ChildrenVisitationPdfDTO.class);
    }

    @Override
    protected String getSqfwFormSummaryType() {
        return SupportBusinessTypeEnum.CHILDREN_VISITATION.getName();
    }
}
